//
//  TraditionalViewController.m
//  PersonalHospital
//
//  Created by qianfeng on 15/12/11.
//  Copyright (c) 2015年 QianFeng. All rights reserved.
//

#import "TraditionalViewController.h"
#import "TranditionalBannerModel.h"
#import "TranditionalBannerCell.h"
#import "BannerMessageViewController.h"
#import "ZJSelectView.h"
#import "TranditionalScrollCell.h"
#import "TranditionalShowModel.h"

#import "AiJiuViewController.h"
#import "AnMoViewController.h"
#import "ShouZuErViewController.h"
#import "BaGuanViewController.h"
#import "GuaShaViewController.h"
#import "TranditionalMoreViewController.h"
#import "TranditionalDetailViewController.h"

#define Select_Tag 55

@interface TraditionalViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)NSMutableArray * bannerArray;

@property(nonatomic,strong)UIScrollView * scrollView;

@end

@implementation TraditionalViewController
{
    AnMoViewController * _anMo;
    GuaShaViewController * _guaSha;
    BaGuanViewController * _baGuan;
    ShouZuErViewController * _shouZuEr;
    AiJiuViewController * _aiJiu;
    
}
-(UIScrollView *)scrollView{
    if (_scrollView==nil) {
        _scrollView=[[UIScrollView alloc]init];
        _scrollView.frame=CGRectMake(0, 0, KScreen_Size.width, self.view.frame.size.height);
        _scrollView.delegate=self;
    }
    return _scrollView;
}

-(NSMutableArray *)bannerArray{
    if (_bannerArray==nil) {
        _bannerArray=[[NSMutableArray alloc]init];
    }
    return _bannerArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewController];
}

-(void)initViewController{
     __weak TraditionalViewController * weakSelf=self;
    
    _anMo=[[AnMoViewController alloc]init];
    
    _aiJiu=[[AiJiuViewController alloc]init];
    
    _baGuan=[[BaGuanViewController alloc]init];
    
    _shouZuEr=[[ShouZuErViewController alloc]init];
    
    _guaSha=[[GuaShaViewController alloc]init];
    
    _anMo.buttonClick=^(NSString * string){
        [weakSelf playVedioWithPath:string];
    };
    //播放视频
    _aiJiu.buttonClick=^(NSString * string){
        [weakSelf playVedioWithPath:string];
    };
    _baGuan.buttonClick=^(NSString * string){
        [weakSelf playVedioWithPath:string];
    };
    _shouZuEr.buttonClick=^(NSString * string){
        [weakSelf playVedioWithPath:string];
    };
    _guaSha.buttonClick=^(NSString * string){
        [weakSelf playVedioWithPath:string];
    };
    
    //点击更多切换界面
    _anMo.buttonMoreClick=^{
        [weakSelf pushView];
    };
    
    //进入详情页
    _anMo.indexPathClick=^(InfoModel * model){
        [weakSelf pushDetail:model];
    };
    
}

-(void)pushDetail:(InfoModel *)model{
    TranditionalDetailViewController * detail=[[TranditionalDetailViewController alloc]init];
    detail.model=model;
    
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)pushView{
    TranditionalMoreViewController * more =[[TranditionalMoreViewController alloc]init];
    [self.navigationController pushViewController:more animated:YES];
}

-(void)playVedioWithPath:(NSString *)string{
    MPMoviePlayerViewController * player=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:string]];
        [self presentMoviePlayerViewControllerAnimated:player];

}
-(void)addTableView{
    NSArray * titles=@[@"按摩",@"刮痧",@"拔罐",@"艾灸",@"手足耳"];
    ZJSelectView * select=[[ZJSelectView alloc]initWithFrame:CGRectMake(0, 0, KScreen_Size.width,40)];
    select.titles=titles;
    select.tag=Select_Tag;
    select.selectAtIndex=^(NSInteger index){
        [self pushWithIndex:index];
    };
    [select loadWithIndex:self.scrollIndex];
    
    self.tableView.tableHeaderView=select;
    
    [super addTableView];
}


-(void)pushWithIndex:(NSInteger)index{
    _scrollIndex=index;
    [self.tableView reloadData];
}
-(void)loadData{
    NSString * path=KTranditional_Banner_Path;
    [self loadDataWithPath:path];
}

-(void)pastDataWith:(id)responseObject{
    NSArray * banner=responseObject[@"banner"];
    
    [self.bannerArray addObjectsFromArray:[TranditionalBannerModel arrayOfModelsFromDictionaries:banner]];
    
    [self.tableView reloadData];
}

#pragma mark --UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
          return KTranditional_Banner_Cell;
    }
    return KTranditional_Scroll_Cell;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell=nil;
    NSInteger index=indexPath.section;
    switch (index) {
        case 0:
            cell=[self tableView:tableView bannerCellForRowAtIndexPath:indexPath];
            break;
        case 1:
            cell=[self tableView:tableView scrollCellForRowAtIndexPath:indexPath];
            break;
        default:
            break;
    }
    return cell;
}

#pragma mark --滚动视图

-(UITableViewCell *)tableView:(UITableView *)tableView scrollCellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TranditionalScrollCell * cell=[tableView dequeueReusableCellWithIdentifier:@"scrollCell"];
    
    if (cell==nil) {
        cell=[[TranditionalScrollCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"scrollCell"];
    }
    NSArray * array=@[_anMo,_guaSha,_baGuan,_aiJiu,_shouZuEr];
    cell.scrollIndex=self.scrollIndex;
    cell.scorll=^(CGFloat scorll){
        [self changeSelect:scorll];
    };
    cell.array=array;
    return cell;
}

-(void)changeSelect:(CGFloat)scorll{
    ZJSelectView * select=(id)[self.view viewWithTag:Select_Tag];
    [select loadWithIndex:scorll];
}

#pragma mark --广告

-(UITableViewCell *)tableView:(UITableView *)tableView bannerCellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TranditionalBannerCell * cell=[tableView dequeueReusableCellWithIdentifier:@"bannercell"];
    if (cell==nil) {
        cell=[[TranditionalBannerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bannercell"];
    }
    _index=cell.index;
    cell.bannerArray=self.bannerArray;
    cell.imageTap=^(NSInteger index){
        [self pushToBannerMessageWithIndex:index];
    };
    return cell;
}

-(void)pushToBannerMessageWithIndex:(NSInteger)index{
    BannerMessageViewController * message=[[BannerMessageViewController alloc]init];
    message.bannerModel=self.bannerArray[index];
    if (index==1) {
        index=2;
    }
    [self.navigationController pushViewController:message animated:YES];

}

//#pragma mark --头视图
//
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (section==1) {
//        return 44;
//    }
//    return 0;
//}
//
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (section==1) {
//        TranditionalShowModel * model=nil;
//        UIView * view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
//        
//        view.backgroundColor=[UIColor groupTableViewBackgroundColor];
//        
//        UILabel * name=[[UILabel alloc]init];
//        name.frame=CGRectMake((KScreen_Size.width-80)/2, 0, 80, view.frame.size.height);
//        name.textAlignment=NSTextAlignmentLeft;
//        name.textColor=[UIColor blackColor];
//        model=self.tranditionalModelArray[0];
//        name.text=model.adultDiseaseName;
//        name.font=[UIFont systemFontOfSize:18];
//        
//        UILabel * right=[[UILabel alloc]init];
//        right.frame=CGRectMake((KScreen_Size.width-80), 0, 80,view.frame.size.height);
//        right.textAlignment=NSTextAlignmentLeft;
//        right.textColor=[UIColor blackColor];
//        right.text=@"更多 > >";
//        right.font=[UIFont systemFontOfSize:14];
//        [view addSubview:name];
//        [view addSubview:right];
//        return view;
//    }
//    return nil;
//}


@end
